GBML manual / Collision

tile_layer_set_active

Switches this room to collision layer number layer, and turns every other layer off. Layer 1 is the room the way you first drew it. Layers 2 and up are the ones you drew after it in the Room Editor. Only one layer is ever on, and it takes effect at once. Switch to a door-open layer and the door opens. Switch back to layer 1 and it shuts.

Syntax

tile_layer_set_active(layer)

Arguments

NameType
layerint

Returns

Nothing.

Description

Switches this room to one of its collision layers, and turns every other layer off. You give it the layer's number. Layer 1 is the room the way you first drew it. Layers 2, 3 and up are the ones you added in the Room Editor's Collision Layers box, in the order you made them.

tile_layer_set_active(2);

You can also write a layer's name instead of its number. See Writing a layer's name below.

Only one layer is on at a time, and the change happens straight away. Switch to a layer with the door open and the door opens. Switch back to layer 1 and it shuts. Collision layers explains how layers are made.

Writing a layer's name

If your project turns on Use layer names in my code (Project Settings, under Collision layers), every layer from 2 up has a word you can write instead of its number. The word is the room's name, then _, then the layer's name:

tile_layer_set_active(west_room_door_open);    // layer 3 of west_room: same as tile_layer_set_active(3)

The build swaps the word for the number. The game never sees the name, and nothing is looked up while you play. Layer 1 has no name, so you write 1 for it. If a word is wrong, the build stops, which a wrong number would not do. Collision layers explains the rules, the names that cannot work, and what happens when you rename a layer or a room.

A word is only a number, so it still switches the room the player is in. east_room_door_open is 2, and if you use it while the player is in west_room, it switches west_room to its layer 2.

Layers do not mix

When you switch, the room is laid down again from the start. Every tile goes back to how layer 1 has it. Then the layer you chose changes its own tiles. Nothing from the layer you left behind stays. Layer 3 never sits on top of layer 2.

So if you want a door and a bridge open at the same time, draw one layer that has both.

What it changes

For each tile the layer lists, the layer says what the tile should block and what it should look like. The game writes both:

A layer does not have to change both. A layer made only of collision changes leaves the pictures alone, and a layer made only of pictures leaves what blocks alone.

Layer numbers, and deleting a layer

The Room Editor adds a layer to the end of the list. It cannot move a layer or put one in the middle. So layer 3 is the same layer, today and after you add ten more. That is why your code and your saved games can use the number.

You can delete a layer, and that is the one time numbers change: every layer after the deleted one moves down by one. The editor checks your code first and offers to change the numbers for you. See Collision layers for the whole story, including the one case that cannot be fixed for you.

Changes you make in code are lost

tile_set_class, tile_set_flags and tile_set_index change one tile at a time. If you change a tile that way and then switch to a different layer, your change is lost. The whole room is laid down again, so the tile goes back to what the new layer says.

Switching to the layer that is already on does nothing at all, so your changes stay. That also means it is safe to call this every frame.

A number that does not exist

If the room has no layer with that number, nothing changes. The game does not stop. It writes a line in the debug log, which you can read in an emulator that shows the log. A number below 1 does the same.

The build cannot tell which room you meant, only that some room has a layer that high.

Only this room

tile_layer_set_active works on the room the player is in right now. There is no room argument, because only that room's tiles are in memory. Two rooms can each have a layer 2, and they do not touch each other.

Saving

The game remembers which layer each room is on. Leave the room and come back, and it is still on that layer. That memory reaches the cartridge when you call save_commit, the same as anything else you save. save_clear forgets it, and every room starts on layer 1 again the next time it loads.

The game finds a room by its number, not its name. Every room gets a number of its own when you make it, and the number never changes, so renaming a room does not lose the layer it was on. If you also use layer names in your code, every layer word in that room changes with the room's name, and the build stops until your code matches. That one you cannot miss.

The memory is kept in a small place of its own. It is not one of the values you save with save_set. So a game that saves a lot of values can never make a door shut itself again.

When it does nothing

Example

// A key lying in the room. When the hero touches it, the door opens for good.
if (pos_meeting(x, y, obj_doc_hero)) {
    tile_layer_set_active(2);   // layer 2 is "door open"
    save_commit();
    instance_destroy(self);
}

The key waits in the room. The moment the hero overlaps it, the room switches to layer 2. The door cells stop blocking and show the open door. Then the game saves, so a power cycle does not shut the door again, and the key removes itself. If the hero leaves the room and comes back, the door is still open and the key is gone.

// Press the interact button to shut the door again, if it is open.
if (action_pressed(interact) && tile_layer_active() == 2) {
    tile_layer_set_active(1);
}

Layer 1 is the room the way you first drew it, with the door shut. tile_layer_active asks first, so the door only shuts when it is open.

On the GBA

Works on the room the player is in right now - there is no room argument, because only that room’s tiles are in memory. The room is laid down again from the start: every tile goes back to how layer 1 has it, then the layer you chose changes its own tiles (what they block and what they look like, including flips and palette). Nothing of the layer you left behind stays, so layers never mix. That also means a change made with tile_set_class, tile_set_flags or tile_set_index is lost when you switch to a different layer. Switching to the layer that is already on does nothing, so it is safe to call every frame, and it keeps those changes. A number this room does not have changes nothing and writes a line to the debug log; the build refuses a typed-out number that no room in the project has. The layer you switch to is remembered for the room, by the room’s name (rename a room and it starts on layer 1 again). It is kept apart from your save_set values, in a small place of its own, so a game full of saved values cannot make it forget. It only reaches the cartridge when you call save_commit, and save_clear puts every room back to layer 1. A room too big to change while the game runs (the build refuses one with layers) cannot switch layers.

See also: tile_layer_activeCollision layerstile_set_classtile_set_indexsave_commitsave_clear